package com.cakebackoffice;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cakebackoffice.Model.ProductType;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: isen
 * Date: 29/02/12
 * Time: 23:48
 * To change this template use File | Settings | File Templates.
 */
public class ListviewProductListAdapter extends ArrayAdapter<ProductType> {

    int resource;

    public ListviewProductListAdapter(Context context, int textViewResourceId, List<ProductType> objects) {
        super(context, textViewResourceId, objects);  //Et les objets aussi, loulou ...
        this.resource = textViewResourceId;

    }


    // On override ArrayAdapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout productView;

        // Récuperer le produit courant
        ProductType productType = getItem(position);

        // Inflate the product details
        if (convertView == null) {
            productView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
            vi.inflate(resource, productView, true);
        } else {
            productView = (LinearLayout) convertView;
        }

        // Récupérer les View des productView
        TextView nomProduit = (TextView) productView.findViewById(R.id.nomproduit2);
        TextView totalHT = (TextView) productView.findViewById(R.id.prixht2);
        ImageView imageProduit = (ImageView) productView.findViewById(R.id.productImage2);

        // les rempliiiir !!
        //GESTION DES MENUS MANQUANTE

        nomProduit.setText(productType.getName().toString());

        //on crée le drawable
        Resources res = getContext().getResources();
        Drawable shape = res.getDrawable(R.drawable.defaut);  //en dur pour l'instant !!!! mouahaha
        imageProduit.setImageDrawable(shape);

        totalHT.setText(new Float((productType.getLocalPrice(null) * (1f + productType.getVatOrDefault() / 100f))).toString() + " €");


        return (productView);


    }

}
